package com.topvision.myviewtest.algorithm.util;

import android.util.SparseArray;

import com.topvision.myviewtest.algorithm.sort.InsertSort;
import com.topvision.myviewtest.algorithm.sort.MergeSort;
import com.topvision.myviewtest.algorithm.sort.SelectionSort;

import java.util.Random;

/**
 * @FileName : SortTestHelper.java
 * @Author   : Zero_Tzz
 * @E-mail   : 497847377@qq.com
 * @Data     : 2018-07-09 23:40
 * @Describe : 算法帮助类 每添加一个算法，只需要添加对应索引和集合就可以运行使用
 */
public class SortTestHelper {

    public static final String METHOD_SORT = "sort";

    public static final String SORT_NAME_KEY = "number";
    public static final String SORT_CLASS_KEY = "class";
    public static final int SORT_SELECT = 0;
    public static final int SORT_INSERT = 1;
    public static final int SORT_MERGE = 2;

    private static StringBuffer stringBuffer;

    private static SparseArray<String> mSortNameArray;
    private static SparseArray<Class> mSortClassArray;

    static {
        init();
        addToArrays();
    }

    private static void addToArrays() {
        mSortNameArray.append(SORT_SELECT, "选择排序");
        mSortNameArray.append(SORT_INSERT, "插入排序");
        mSortNameArray.append(SORT_MERGE, "归并排序");
        mSortClassArray.append(SORT_SELECT, SelectionSort.class);
        mSortClassArray.append(SORT_INSERT, InsertSort.class);
        mSortClassArray.append(SORT_MERGE, MergeSort.class);
    }

    private static void init() {
        stringBuffer = new StringBuffer();
        mSortNameArray = new SparseArray<>();
        mSortClassArray = new SparseArray<>();
    }

    /**
     * 生成随机数数组
     */
    public static Integer[] generateRandomArray(int n, int rangeLeft, int rangeRight) {
        int diff = rangeRight - rangeLeft + 1;
        if (diff <= 1) {
            throw new IllegalArgumentException("rangeRight <= rangeLeft");
        }
        Integer[] arr = new Integer[n];
        Random random = new Random();
        for (int i = 0; i < n; i++) {
            arr[i] = rangeLeft + random.nextInt(diff);
        }
        return arr;
    }

    public static <T> void printArray(boolean sortFlag, T[] arr) {
        stringBuffer.delete(0, stringBuffer.length());
        stringBuffer.append(sortFlag ? "排序后: " : "排序前: ");
        for (T t : arr) {
            stringBuffer.append(t).append(" ");
        }
        System.out.println(stringBuffer.toString());
    }

    public static String getSortNameValue(Integer sortNumber) {
        return mSortNameArray.get(sortNumber);
    }

    public static SparseArray<String> getmSortNameArray() {
        return mSortNameArray;
    }

    public static Class getSortClassValue(Integer sortNumber) {
        return mSortClassArray.get(sortNumber);
    }

    public static SparseArray<Class> getmSortClassArray() {
        return mSortClassArray;
    }
}
